$(function () {
    $('#save-goods-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#goods-dialog');
        var name = $("input[name='name']").val();
        var goods_img = $("input[name='goods_img']").val();
        var old_price = $("input[name='old_price']").val();
        var goods_code = $("input[name='goods_code']").val();
        // var stock_t = $("input[name='stock_t']").val();
        var price = $("input[name='price']").val();
        var stock_t = $("input[name='stock_t']").val();
        var freight = $("input[name='freight']").val();
        // var integral = $("input[name='integral']").val();
        // var rich_text = $("input[name='rich_text']").val();
        var priority = $("input[name='priority']").val();


        // var type_id = document.getElementsByName("type_id")[0].value;
        var label_id = document.getElementsByName("label_id")[0].value;
        // var index_status = document.getElementsByName("index_status")[0].value;
        var submitType = self.attr("data-type");
        var goods_id = self.attr('data-id');

        var url = '/cms/mall/update_goods/';

        zlajax.post({
            'url':url,
            'data':{
                'name':name,
                'goods_img':goods_img,
                'old_price':old_price,
                'goods_code':goods_code,
                'stock_t':stock_t,
                'price':price,
                'freight':freight,
                // 'integral':integral,
                // 'rich_text':rich_text,
                'priority':priority,
                // 'type_id':type_id,
                'label_id':label_id,
                'goods_id':goods_id,
                // 'index_status':index_status,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".edit-goods-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#goods-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var name = tr.attr("data-name");
        // var price_info = tr.attr("data-info");
        var price = tr.attr("data-price");
        // var integral = tr.attr("data-integral");
        var goods_img = tr.attr("data-img");
        var goods_code = tr.attr("data-code");
        var old_price = tr.attr("data-old");
        var priority = tr.attr("data-priority");
        var freight = tr.attr("data-freight");
        var stock_t = tr.attr("data-stock_t");
        // var num = tr.attr("data-num");



        var nameInput = dialog.find("input[name='name']");
        var imageInput = dialog.find("input[name='goods_img']");
        var codeInput = dialog.find("input[name='goods_code']");
        // var infoInput = dialog.find("input[name='price_info']");
        var oldInput = dialog.find("input[name='old_price']");
        var priceInput = dialog.find("input[name='price']");
        var freightInput = dialog.find("input[name='freight']");
        var stockInput = dialog.find("input[name='stock_t']");
        // var integralInput = dialog.find("input[name='integral']");

        var priorityInput = dialog.find("input[name='priority']");
        // var richInput = dialog.find("input[name='rich_text']");
        // var fromInput = dialog.find("input[name='from_id']");
        var savebtn = dialog.find("#save-goods-btn");

        nameInput.val(name);
        imageInput.val(goods_img);
        codeInput.val(goods_code);
        oldInput.val(old_price);
        // infoInput.val(price_info);
        priceInput.val(price);
        stockInput.val(stock_t);
        freightInput.val(freight);
        // integralInput.val(integral);
        priorityInput.val(priority);
        // richInput.val(rich_text);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.delete-goods-btn').click(function (event) {
        var self = $(this)
        var tr = self.parent().parent();
        var goods_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这个商品吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/mall/del_goods/',
                    'data':{
                        'goods_id':goods_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                        }
                    }
                })
            }
        })
    })
});



// 商品或类型展示在其他页面
$(function () {
    $('.to_show').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var tr = self.parent().parent();
        var show_id = tr.attr('data-id');

        zlajax.post({
            'url': '/cms/mall/goods_to_type/',
            'data':{
                'show_id': show_id,
                'status': 1,
                'is_type': 1,
            },
            'success':function (data) {
                if(data['code']==200){
                    // zlalert.alertSuccessToast('操作成功');
                    window.location.reload()
                }else{
                    const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});

// 不展示
$(function () {
    $('.not_show').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var tr = self.parent().parent();
        var show_id = tr.attr('data-id');

        zlajax.post({
            'url': '/cms/mall/goods_to_type/',
            'data':{
                'show_id': show_id,
                'status': 1,
                'is_type': 0,
            },
            'success':function (data) {
                if(data['code']==200){
                    // zlalert.alertSuccessToast('操作成功');
                    window.location.reload()
                }else{
                    const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});
